local width = Width
local height = Height

local bounceObject = {
    x = 0,
    y = 0,
    width = 0,
    height = 0,
    speed = 0,
    angle = 0
}

local dx, dy

local function calculateBounce(ow, oh, w, h)
    -- 垂直方向的碰撞检测与反弹
    if bounceObject.x <= 0 then
        bounceObject.x = 0
        bounceObject.angle = math.pi - bounceObject.angle
    elseif bounceObject.x + ow >= w then
        bounceObject.x = w - ow
        bounceObject.angle = math.pi - bounceObject.angle
    end

    -- 水平方向的碰撞检测与反弹
    if bounceObject.y <= 0 then
        bounceObject.y = 0
        bounceObject.angle = - bounceObject.angle
    elseif bounceObject.y + oh >= h then
        bounceObject.y = h - oh
        bounceObject.angle = - bounceObject.angle
    end
end

local function init(x, y, w, h)
    bounceObject.x = x
    bounceObject.y = y
    bounceObject.width = w
    bounceObject.height = h
    bounceObject.speed = width / 3
    bounceObject.angle = math.random() * math.pi * 2
end

local function update(dt)
    dx = math.cos(bounceObject.angle) * bounceObject.speed * dt
    dy = math.sin(bounceObject.angle) * bounceObject.speed * dt
    bounceObject.x = bounceObject.x + dx
    bounceObject.y = bounceObject.y+ dy

    calculateBounce(bounceObject.width, bounceObject.height, width, height)
end

return{
    init = init,
    update = update,
    getX = function() return bounceObject.x end,
    getY = function() return bounceObject.y end
}